package com.ruoyi.buy.service.impl;

import com.ruoyi.buy.domain.entity.PO.BuyMaterialOtherItem;
import com.ruoyi.buy.domain.entity.PO.BuySpaMainInfoPO;
import com.ruoyi.buy.domain.entity.buy0001.BuySpa;
import com.ruoyi.buy.mapper.PO.BuyMaterialOtherItemMapper;
import com.ruoyi.buy.mapper.PO.BuySpaMainInfoMapper;
import com.ruoyi.buy.service.IBuy0001Service;
import com.ruoyi.buy.service.IBuyMaterialOtherItemService;
import com.ruoyi.buy.service.ICommonService;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 材料清单明细-替代Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-02-20
 */
@Service
@Transactional
public class BuyMaterialOtherItemServiceImpl implements IBuyMaterialOtherItemService
{
    @Resource
    private BuyMaterialOtherItemMapper buyMaterialOtherItemMapper;

    @Resource
    private ICommonService iCommonService;

    @Resource
    private BuySpaMainInfoMapper buySpaMainInfoMapper;

    @Resource
    private IBuy0001Service buy0001Service;

    /**
     * 查询材料清单明细-替代
     * 
     * @param id 材料清单明细-替代主键
     * @return 材料清单明细-替代
     */
    @Override
    public BuyMaterialOtherItem selectBuyMaterialOtherItemById(Long id)
    {
        return buyMaterialOtherItemMapper.selectBuyMaterialOtherItemById(id);
    }

    /**
     * 查询材料清单明细-替代列表
     * 
     * @param buyMaterialOtherItem 材料清单明细-替代
     * @return 材料清单明细-替代
     */
    @Override
    public List<BuyMaterialOtherItem> selectBuyMaterialOtherItemList(BuyMaterialOtherItem buyMaterialOtherItem)
    {
        return buyMaterialOtherItemMapper.selectBuyMaterialOtherItemList(buyMaterialOtherItem);
    }

    /**
     * 新增材料清单明细-替代
     * 
     * @param buyMaterialOtherItem 材料清单明细-替代
     * @return 结果
     */
    @Override
    public int insertBuyMaterialOtherItem(BuyMaterialOtherItem buyMaterialOtherItem)
    {
        buyMaterialOtherItem.setCreateTime(DateUtils.getNowDate());
        return buyMaterialOtherItemMapper.insertBuyMaterialOtherItem(buyMaterialOtherItem);
    }

    /**
     * 修改材料清单明细-替代
     * 
     * @param buyMaterialOtherItem 材料清单明细-替代
     * @return 结果
     */
    @Override
    public int updateBuyMaterialOtherItem(BuyMaterialOtherItem buyMaterialOtherItem)
    {
        buyMaterialOtherItem.setUpdateTime(DateUtils.getNowDate());
        return buyMaterialOtherItemMapper.updateBuyMaterialOtherItem(buyMaterialOtherItem);
    }

    /**
     * 批量删除材料清单明细-替代
     * 
     * @param ids 需要删除的材料清单明细-替代主键
     * @return 结果
     */
    @Override
    public int deleteBuyMaterialOtherItemByIds(Long[] ids)
    {
        return buyMaterialOtherItemMapper.deleteBuyMaterialOtherItemByIds(ids);
    }

    /**
     * 删除材料清单明细-替代信息
     * 
     * @param id 材料清单明细-替代主键
     * @return 结果
     */
    @Override
    public int deleteBuyMaterialOtherItemById(Long id)
    {
        return buyMaterialOtherItemMapper.deleteBuyMaterialOtherItemById(id);
    }

    @Override
    public int saveSuggestDialog(List<BuyMaterialOtherItem> para) throws Exception {
        for (BuyMaterialOtherItem item:para
             ) {
            if (item.getId() == null || item.getId() == 0){
                item.setCreateTime(DateUtils.getNowDate());
                item.setCreateBy(iCommonService.getUsername());
                this.buyMaterialOtherItemMapper.insertBuyMaterialOtherItem(item);
            }else{
                item.setUpdateTime(DateUtils.getNowDate());
                item.setUpdateBy(iCommonService.getUsername());
                this.buyMaterialOtherItemMapper.updateBuyMaterialOtherItem(item);
            }
            //处理价格表
            this.dealSpaPriceInfo(item);
        }
        return 1;
    }

    private void dealSpaPriceInfo(BuyMaterialOtherItem item) throws Exception {
//先查询零件是否存在
        BuySpaMainInfoPO searchPara = new BuySpaMainInfoPO();
        searchPara.setvSpaName(item.getvSpaName());
        searchPara.setvSpaBrand(item.getvSpaBrand());
        searchPara.setvSpaModel(item.getvSpaModel());
        BuySpaMainInfoPO spa  = this.buySpaMainInfoMapper.selectSpaInfoByCondition(searchPara);
        BuySpa param = new BuySpa();
        com.ruoyi.common.utils.bean.BeanUtils.copyBeanProp(param,item);
        if (spa == null || spa.getiD() == null ){
            param.setCreateBy(iCommonService.getUsername());
            param.setvKind(item.getvKind());
            this.buy0001Service.insertSpaInfo(param);
        }else{
            param.setUpdateBy(iCommonService.getUsername());
            this.buy0001Service.updateSpaInfo(param);
        }

    }
}
